package com.graduation.springbootinit.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation.springbootinit.model.entity.ShopPart.Order;
import com.graduation.springbootinit.model.entity.ShopPart.OrderItem;
import com.graduation.springbootinit.service.ClothingProductService;
import com.graduation.springbootinit.mapper.OrderMapper;
import com.graduation.springbootinit.mapper.OrderItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderTimeoutJob {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private ClothingProductService productService;

//    /**
//     * 每分钟检查一次超时订单
//     */
//    @Scheduled(cron = "0 */1 * * * ?")
//    @Transactional(rollbackFor = Exception.class)
//    public void handleTimeoutOrders() {
//        // 查找超时未支付的订单
//        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("status", 0)  // 待支付状态
//                .lt("created_at", new Date(System.currentTimeMillis() - Order.ORDER_TIMEOUT));  // 创建时间超过30分钟
//
//        List<Order> timeoutOrders = orderMapper.selectList(queryWrapper);
//
//        for (Order order : timeoutOrders) {
//            try {
//                // 更新订单状态为超时关闭(5)
//                order.setStatus(5);
//                order.setUpdatedAt(new Date());
//                orderMapper.updateById(order);
//                log.info("订单{}已超时关闭", order.getOrderNo());
//            } catch (Exception e) {
//                log.error("处理超时订单{}失败", order.getOrderNo(), e);
//            }
//        }
//    }
} 